<script>
import axios from "axios";
import {getToken} from "@/store/token";

export default {
  name: "userSafety",
  data() {
    var validateOriginalPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.state.password.checkPass !== '') {
          this.$refs.passwordForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.state.password.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var checkMobile = (rule, value, callback) => {
      const regUser = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
      if (regUser.test(value)) {
        return callback()
      }else {
        if (value.length !== 11) {
          callback(new Error('手机号码长度应该是11位'))
        }
        callback(new Error("手机号格式不正确"))
      }
    };
    return {
      activeName: "first",
      state: {
        password: {
          pass: '',
          checkPass: '',
          originalPass: '',
          phone: '',
          newPhone: ''
        }
      },
      rules: {
        originalPass: [
          {required: true, validator: validateOriginalPass, trigger: 'blur'}
        ],
        pass: [
          {required: true, validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {required: true, validator: validatePass2, trigger: 'blur'}
        ],
        phone: [{required: true, validator: checkMobile, trigger: 'blur'}],
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs["passwordForm"].validate((valid) => {
        if (valid) {
          if (this.state.password.originalPass !== '') {
            this.$api.user.updateUserPassword({
              id: JSON.parse(getToken("user")).id,
              password: this.state.password.originalPass,
              newPassword: this.state.password.checkPass
            }).then((res) => {
              if (res.data.content) {
                this.$message({
                  message: '密码修改成功',
                  type: 'success'
                });
                this.$refs["passwordForm"].resetFields();
              } else {
                this.$message.error('原始密码错误' + res);
              }
            }).catch((res) => {
              this.$message.error('请求失败' + res);
            })
          }
          if (this.state.password.phone !== '') {
            this.$api.user.updateUserPhone({
              id: JSON.parse(getToken("user")).id,
              phone: this.state.password.phone,
              newPhone: this.state.password.newPhone
            }).then((res) => {
              if (res.data.success) {
                this.$message({
                  message: '手机号修改成功',
                  type: 'success'
                });
                this.$refs["passwordForm"].resetFields();
              } else {
                this.$message.error(res.data.message);
              }
            }).catch((res) => {
              this.$message.error('请求失败' + res);
            })
          }

        } else {
          console.log('error submit!!');
          return false;
        }

      });
    },
    resetForm() {
      this.$refs["passwordForm"].resetFields();
    },
  }
}
</script>

<template>
  <div>
    <span style="font-size: 18px;">账户安全</span>
    <el-tabs v-model="activeName" >
      <el-tab-pane label="修改密码" name="first">
        <div class="safety-body" v-if="activeName==='first'">
          <div>
            <el-form :model="state.password" status-icon :rules="rules" ref="passwordForm" label-width="100px"
                     class="demo-ruleForm">
              <el-form-item label="原始密码" prop="originalPass">
                <el-input type="password" v-model="state.password.originalPass" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="新密码" prop="pass">
                <el-input type="password" v-model="state.password.pass" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="state.password.checkPass" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="resetForm()">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="绑定手机" name="second">
        <div class="safety-body" v-if="activeName==='second'">
          <div>
            <el-form :model="state.password" status-icon :rules="rules" ref="passwordForm" label-width="120px"
                     class="demo-ruleForm">
              <el-form-item label="原始手机号码" prop="phone">
                <el-input v-model="state.password.phone" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="新手机号" prop="phone">
                <el-input v-model="state.password.newPhone" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="resetForm()">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="注销账户" name="three">
        <div class="safety-body">
          <div>
            <span>注销账户</span>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<style scoped>
.safety-body {
  padding: 13px 20px;
  height: 400px;
  border: 1px solid #E5E5E5;
}
</style>